<template>
  <!--列表-->
  <router-exact-view name="announce">
    <div style="width: 100%" class="template-list-page-container">
      <div class="search-area">
        <search-form-container>
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="状态">
              <el-select
                v-model="searchForm.status"
                placeholder="请选择"
              >
                <el-option label="全部" :value="-1" />
                <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="handlePageChange(1)"
              >查 询</el-button>
            </el-form-item>
          </el-form>
        </search-form-container>
      </div>
      <my-table
        v-loading="loading"
        :columns="columns"
        :data-source="data"
        :pagination="pagination"
        :btn-options="btnOptions"
        title="公告列表"
        @page-change="handlePageChange"
      >
        <div slot="table-btn">
          <el-button type="primary" @click="addAnnounce">新增公告</el-button>
        </div>
      </my-table>
    </div>
  </router-exact-view>
</template>

<script>
import MyTable from '@/components/my-table/index'
import SearchFormContainer from '@/components/search-form-container/index'
import RouterExactView from '@/components/routerExactView'
import { getAnnounceList, changeStatus, delAnnounce } from '@/api/operate'
import { announceColumns } from '../columns'
import { statusOptions } from '../attr'

export default {
  components: { RouterExactView, MyTable, SearchFormContainer },
  data() {
    const that = this
    return {
      statusOptions,
      activeName: '1',
      /* 列表相关 */
      columns: announceColumns,
      data: [],
      searchForm: {},
      loading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      btnOptions: [
        {
          name: '查看详情',
          handler: function(row) {
            that.$router.push({
              path: `/operate/announce/detail/${row.id}`
            })
          }
        },
        {
          name: '编辑',
          v_if: row => row.status === 0 || row.status === 2,
          handler: function(row) {
            that.$router.push({
              path: `/operate/announce/edit/${row.id}`
            })
          }
        },
        {
          name: '发布',
          v_if: row => row.status === 0 || row.status === 2,
          handler: function(row) {
            that.changeAnnounceStatus(row, 1)
          }
        },
        {
          name: '下架',
          v_if: row => row.status === 1,
          handler: function(row) {
            that.changeAnnounceStatus(row, 2)
          }
        },
        {
          name: '删除',
          v_if: row => row.status === 0 || row.status === 2,
          handler: function(row) {
            that.deleteAnnounce(row)
          }
        }
      ]
    }
  },
  mounted() {
    if (this.$route.name === 'announce') {
      this.handlePageChange()
    }
  },
  methods: {
    addAnnounce() {
      this.$router.push({ path: `/operate/announce/add` })
    },
    async handlePageChange(current, size) {
      if (current) {
        this.pagination.current = current
      }
      if (size) {
        this.pagination.pageSize = size
      }
      this.loading = true
      try {
        const params = JSON.parse(JSON.stringify(this.searchForm || {}))
        if (params.status === -1) {
          delete params.status
        }
        const res = await getAnnounceList({
          ...params,
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize
        })
        if (res && res.list) {
          this.data = res.list
          this.pagination = {
            current: res.pageNum,
            pageSize: res.pageSize,
            total: res.totalElements
          }
        }
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    async changeAnnounceStatus(row, status) {
      const that = this
      this.$confirm(`确定要${status === 1 ? '发布' : '下架'}该公告吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        try {
          const res = await changeStatus({
            id: row.id,
            status
          })
          if (res) {
            that.$message({
              message: `${status === 1 ? '发布' : '下架'}公告成功！`,
              type: 'success'
            })
            that.handlePageChange(that.pagination.current)
          }
        } catch (err) {
          console.log('接口出错')
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    async deleteAnnounce(row) {
      const that = this
      this.$confirm('确定要删除该公告吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        try {
          const res = await delAnnounce({ id: row.id })
          if (res) {
            that.$message({
              message: `删除公告成功！`,
              type: 'success'
            })
            that.handlePageChange(that.pagination.current)
          }
        } catch (err) {
          console.log('接口出错')
        }
      }).catch(() => {
        console.log('取消')
      })
    }
  }
}
</script>
<style scoped lang="less">
@import "../../common.css";
/* 查询条件容器的样式 */
.search-area {
  padding: 16px 16px 0;
  margin-bottom: 8px;
  background: #fff;
}
.tab-list {
  background: #fff;
  padding: 15px 15px 0;
  overflow: hidden;
  .tab-list-title {
    float: left;
    color: #303133;
    font-size: 16px;
    font-weight: bold;
    line-height: 32px;
  }
  .tab-list-btn {
    float: right;
  }
}
.el-tabs-list {
  background: #fff;
  padding: 0 15px;
  .child-my-table {
    ::v-deep .my-table-area {
      padding: 0;
    }
  }
}
</style>
